<?php

namespace Viettrans\FrontBundle\Services;

class MyFormValidator {
    public function isEmailValid($email, $required = '') {
        $email = strtoupper($email);
        if($required === '') $required = true;  
        
        if ($email == '' && $required == false) return true;
        
        return preg_match("/^[-a-z0-9!#$%&'*+\/=?^_`{|}~]+(\.[-a-z0-9!#$%&'*+\/=?^_`{|}~]+)*@(([a-z0-9]([-a-z0-9]*[a-z0-9]+)?){1,63}\.)+([a-z0-9]([-a-z0-9]*[a-z0-9]+)?){2,63}$/i", $email);
    }
    
    public function isTelephoneValid($telephone, $required = '') {
        $telephone = strtoupper($telephone);
        if($required === '') $required = true;

        if ($telephone == '' && $required == false) return true;

        return preg_match('/^\d{8,15}$/', $telephone);
    }
    
    public function isTextValid($text, $maxLength, $required = '') {
        $text = strtoupper($text);
        if($required === '') $required = true;
        
        if ($text == '' && $required == false) return true;
        
        return (strlen($text) <= $maxLength);
    }
}